// --------------------------------------------------------------------
//   name 	: Inline distance calculator
//   desc 	: Calculate the distance from your villages to the selected village
//   version 	: 0.3
//   dev 	: Mog, Timas
// --------------------------------------------------------------------
// DEPENDENCIES
//   Depends on plugins.enhanceOverview.js in order to retrieve the village list
//   Depends on quickattack for rallypoint information
// --------------------------------------------------------------------
// CHANGE HISTORY
// 0.1 	Initial Release
// 0.3  Release with Premium groups incorporated
// --------------------------------------------------------------------
plugins.distancecalc = { // Calculate the time to travel to a village

// --------------------------------------------------------------------
// Function : get_distance
// --------------------------------------------------------------------
	get_distance : function(from, to) {
		var distance_x = from[0]-to[0];
		var distance_y = from[1]-to[1];
		return Math.round(Math.sqrt(Math.pow(distance_x,2) + Math.pow(distance_y,2))*100)/100;
	},

// --------------------------------------------------------------------
// Function : get_distance_table
// --------------------------------------------------------------------
	'get_distance_table' : function(target) {
		html = "<table>";
		html += "<tr><th>Village name</th><th>Distance</th>";
		
		if (new String(getValuePlugin('UseRallyPoint')) == 'true') {
			if(plugins.quickattack) html += plugins.quickattack.get_overview_heading();
		}

		if (twe.Premium) {
			html += "<th>Groups</th>";
		}
		
		html += "</tr>";

		var villages = twe.myVillages();
		var arr = [];
		var count = 0;

		for(var i in villages) {

			v = villages[i];
			html2 = '';

			if(!v.name) continue;


			html2 += "<tr><td><a href=\"javascript:location.href=location.href.replace(/village=([^&]+)/, 'village=" + v.id+"')\">"+v.name+"</a></td>";
			var distance = plugins.distancecalc.get_distance([v.x,v.y],target);
			html2 += "<td>"+Math.round(distance*100)/100+"</td>";

			if (new String(getValuePlugin('UseRallyPoint')) == 'true') {
				if(plugins.quickattack) html2 += plugins.quickattack.get_overview_row(v.id);
			}
	
			if (twe.Premium) {
				html2 += "<td>"+v.premGroups+"</td>";
			}

			html2 += "</tr>";
			arr.push({'distance':distance,'html':html2});
			count++;
		}

		setFunc("distancecalctoggle", function() {
			$(".distancecalchide").toggle();
			if ($("#distancecalclink span").html() != 'show') { $("#distancecalclink span").html('show'); } else { $("#distancecalclink span").html('hide'); }
			
		});

		arr.sort(function(a,b) {return a.distance-b.distance;});

		for(var i=0; i<arr.length; i++) {
			if (i > 4) {
				arr[i].html = arr[i].html.replace(/^<tr>/g, '<tr class="distancecalchide" style="display: none;">');
			} else {
				arr[i].html = arr[i].html.replace(/^<tr>/g, '<tr class="">');
			}

			if (i%2 == 1) {
				arr[i].html = arr[i].html.replace(/^<tr class="/g, '<tr class="odd ');
			}

			html += arr[i].html;
		}

		html += '<tr><td colspan="14"><a id="distancecalclink" href="javascript:distancecalctoggle();"><span>show</span> all villages</a></td></tr>';
		html += "</table>";
		return html;
	},

// --------------------------------------------------------------------
// Function : enhance_game_info_village
// --------------------------------------------------------------------

	'enhance_game_info_village' : function() {
		target = $("table.main table.vis:last tr td:contains(|)").html().replace(/(.+)\((-?\d+\|-?\d+)\)\s(.+)/gi,"$2").split("|");
		$("table.main table.vis:last").after(plugins.distancecalc.get_distance_table(target));
	},


// --------------------------------------------------------------------
// Function : enhance_game_report
// --------------------------------------------------------------------
	'enhance_game_report' : function() {
		if ($("a[href*=info_village]").length != 2) { return; }
		if ($("a[href*=info_player]").length > 2) { 
			var target = $("a[href*=info_village]:last").html().replace(/(.+)\((-?\d+\|-?\d+)\)(?: (K\d+))?/gi,"$2").split("|");
			$("table.main table.vis:eq(1) td:eq(6)").append("<h4>Distances to "+$("a[href*=info_village]:last").html()+"</h4>"+plugins.distancecalc.get_distance_table(target));z
		} else {
			var target = $("a[href*=info_village]:last").html().replace(/(.+)\((-?\d+\|-?\d+)\)(?: (K\d+))?/gi,"$2").split("|");
			$("table.main table.vis:eq(1) td:eq(2)").append("<h4>Distances to "+$("a[href*=info_village]:last").html()+"</h4>"+plugins.distancecalc.get_distance_table(target));
		}
	},

// --------------------------------------------------------------------
// Variable object
// --------------------------------------------------------------------

	'variables' : {
		'tooltip':["Check to show and use rally point information"],
		'desc':["Show/hide rally point information in the distance list."],
		UseRallyPoint:true,
	},


// --------------------------------------------------------------------
// Information array
// --------------------------------------------------------------------
	'info' : {
		'mandatory' : false,
		'name' : "Inline distance calculator",
		'desc' : "Show your villages sorted on distance, closest first. For Premium users it will show the associated groups for the villages.",
		'version' : "0.3",
		'dev' : "Mog & Timas"
	}
};